package com.yljk.homedoctor.view.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yilijk.base.network.RequestParams;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yljk.homedoctor.R;
import com.yljk.servicemanager.constants.UrlApi;

public class PersonalTaxDialog extends Dialog {


    public PersonalTaxDialog(@NonNull Context context) {
        super(context);
    }

    public PersonalTaxDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected PersonalTaxDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder{
        private Context context;
        private String PersonalTax_title;
        private String PersonalTax_message;
        private String PersonalTax_message1;
        private String disAgreeText;
        private String agreeText;
        private ImageView icon;
        private View contentView;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private View layout;
        private OnClickListener closeButtonClickListener;

        private int status;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.PersonalTax_message = message;
            return this;
        }

        public Builder setMessage1(String message1) {
            this.PersonalTax_message1 = message1;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param
         * @return
         */
        public Builder setMessage(int message) {
            this.PersonalTax_message = (String) context.getText(message);
            return this;
        }

        public Builder setMessage1(int message1) {
            this.PersonalTax_message1 = (String) context.getText(message1);
            return this;
        }

        /***
         * 设置dialog状态来改变icon显示样式
         * 0 无状态
         * 1 失败
         * 2 开启
         * @param status
         * @return
         */
        public Builder setIcon(int status){
           this.status = status;
            return this;
        }


        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.PersonalTax_title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.PersonalTax_title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @return
         */

        public Builder setCloseButtom(OnClickListener listener){
            this.closeButtonClickListener = listener;
            return this;
        }


        public Builder setPositiveButton(int positiveButtonText,
                                         OnClickListener listener) {
            this.agreeText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.agreeText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.disAgreeText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.disAgreeText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public PersonalTaxDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final PersonalTaxDialog dialog = new PersonalTaxDialog(context, R.style.Dialog);
            layout = inflater.inflate(R.layout.dialog_personaltax, null);
            dialog.addContentView(layout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.personalTax_title)).setText(PersonalTax_title);

            // set the confirm button
            if (agreeText != null) {
                ((TextView) layout.findViewById(R.id.personalTax_agree))
                        .setText(agreeText);
                if (positiveButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.personalTax_agree))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            }
            else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.personalTax_agree).setVisibility(
                        View.GONE);
            }

                layout.findViewById(R.id.dialog_personaltax_close).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RequestParams jsonObject = new RequestParams();
                        jsonObject.put("state",3);
                        HttpUtils.post(UrlApi.UpdateState, jsonObject, new HttpCallBack<String>() {
                            @Override
                            public void onSuccess(String result, int code) {

                            }

                            @Override
                            public void onFailure(String failureMsg, int statusCode) {

                            }
                        });
                        dialog.dismiss();
                    }
                });

//            // set the cancel button
//            if (disAgreeText != null) {
//                ((TextView) layout.findViewById(R.id.PersonalTax_disagree))
//                        .setText(disAgreeText);
//                if (negativeButtonClickListener != null) {
//                    ((TextView) layout.findViewById(R.id.PersonalTax_disagree))
//                            .setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    negativeButtonClickListener.onClick(dialog,
//                                            DialogInterface.BUTTON_NEGATIVE);
//                                }
//                            });
//                }
//            }else {
//                // if no confirm button just set the visibility to GONE
//                layout.findViewById(R.id.PersonalTax_disagree).setVisibility(
//                        View.GONE);
//            }
            // set the content message
            if (PersonalTax_message != null) {
//                TextView textView = layout.findViewById(R.id.personalTax_message);
//                textView.setText(PersonalTax_message);
//                RichText.from(PersonalTax_message).bind(this)
//                        .showBorder(false)
//                        .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)
//                        .into((TextView) layout.findViewById(R.id.PersonalTax_message));
                ((TextView) layout.findViewById(R.id.personalTax_message)).setText(PersonalTax_message);
            }
//            else if (contentView != null) {
////                // if no message set
////                // add the contentView to the dialog body
//                ((LinearLayout) layout.findViewById(R.id.content))
//                        .removeAllViews();
//                ((LinearLayout) layout.findViewById(R.id.content))
//                        .addView(contentView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
//            }

            if (PersonalTax_message1!=null){
                ((TextView) layout.findViewById(R.id.personalTax_message1)).setText(PersonalTax_message1);
            }


            dialog.setContentView(layout);

            return dialog;
        }

    }





}
